package com.shhm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.common.entity.admin.SysRoleOperatePerms;
import com.shhm.common.mapper.AuthWhiteMapper;
import com.shhm.admin.service.AuthWhiteService;
import com.shhm.admin.util.RequestUtil;
import com.shhm.common.dto.request.RequestList;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.entity.system.SysPassAuth;
import com.shhm.common.mapper.SysRoleOperatePermsMapper;
import com.shhm.common.utils.MybatisPlusUtil;
import com.shhm.common.utils.RedisUtil;
import com.shhm.common.utils.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/9/7 下午8:26
 */
@Service
public class AuthWhiteServiceImpl implements AuthWhiteService {
    @Resource
    private AuthWhiteMapper authWhiteMapper;
    @Resource
    private SysRoleOperatePermsMapper roleOperatePermsMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public AjaxResultPlus pageList(Integer pageNum, Integer pageSize, Integer isPage, HttpServletRequest request) {
//        if (isPage==0) {
//            LambdaQueryWrapper<SysPassAuth> qw = new LambdaQueryWrapper<SysPassAuth>().select(SysPassAuth::getMenuId, SysPassAuth::getMenuName,SysPassAuth::getName);
//            List<SysPassAuth> list = authWhiteMapper.selectList(qw);
//            return AjaxResultPlus.success(list,false);
//        }
        PageDTO<SysPassAuth> userPage = new PageDTO<>(pageNum, pageSize);
        String userOperatePerms = RequestUtil.getUserOperatePerms(request, roleOperatePermsMapper);
//        if (userOperatePerms.equals("NOT_PERMISSION")) return AjaxResultPlus.error(403,"您无权查看数据");
        PageDTO<SysPassAuth> list=authWhiteMapper.selectPage(userPage, null);
        return AjaxResultPlus.custom(200, false)
                .put("data",list)
                .put("operateStr",userOperatePerms);
    }

    @Override
    public AjaxResultPlus pageSearchList(Integer pageNum, Integer pageSize, SysPassAuth entity) {
        PageDTO<SysPassAuth> userPage = new PageDTO<>(pageNum, pageSize);
        QueryWrapper<SysPassAuth> qw = MybatisPlusUtil.buildSearchQueryWrapper(entity);
        PageDTO<SysPassAuth> list = authWhiteMapper.selectPage(userPage, qw);
        return AjaxResultPlus.success(list,false);
    }

    @Transactional
    @Override
    public AjaxResultPlus add(SysPassAuth entity) {
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(authWhiteMapper.insert(entity));
        flushRedisAboutRole();
        return res;
    }

    @Transactional
    @Override
    public AjaxResultPlus update(SysPassAuth entity) {
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(authWhiteMapper.updateById(entity));
        flushRedisAboutRole();
        return res;
    }

    @Transactional
    @Override
    public AjaxResultPlus delete(RequestList ids) {
        List<Object> list = ids.getList();
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(authWhiteMapper.deleteByIds(list));
        flushRedisAboutRole();
        return res;
    }

    private void flushRedisAboutRole(){
        List<SysPassAuth> whiteAuthList = authWhiteMapper.selectList(null);
        RedisUtil.flushSysAuthWhite(redisTemplate,whiteAuthList);
    }
}
